<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>批量质检出库</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
			//$("#name").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});
		
		function delRow(obj, prefix){
			var id = $(prefix+"_id");
			var delFlag = $(prefix+"_delFlag");
			if (delFlag.val() == ""){
				$(obj).parent().parent().remove();
			}
		}
		
		function addRow(list, idx, tpl, row){
			
			$(list).append(Mustache.render(tpl, {
				idx: idx, delBtn: true, row: row
			}));
			$(list+idx).find("select").each(function(){
				$(this).val($(this).attr("data-value"));
			});
			
			$(list+idx).find("input[type='checkbox']").each(function(){
				var ss = $(this).attr("data-value");
				for (var i=0; i<ss.length; i++){
					if($(this).val() == ss[i]){
						$(this).attr("checked","checked");
					}
				}
			});
			
		}
		
		function changePrice(n,m,k){
			
			var salesPrice = $(n).val();
			var activePrice = $(m).val();
			if(parseFloat(salesPrice)>=parseFloat(activePrice)){
				$(k).val(parseFloat(salesPrice)-parseFloat(activePrice));
			}else{
				$(m).val("0.0");
				$(k).val(salesPrice);
			}
		}
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li class="active"><a href="${ctx}/ka/productManage/">批量质检出库</a></li>
	</ul>
	<form:form id="inputForm" modelAttribute="productManage" action="${ctx}/ka/productManage/batchQualityOut" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>
		<div class="control-group">
			<table id="contentTable" class="table table-striped table-bordered table-condensed">
				<thead>
					<tr>
						<th>品类</th>
						<th>货号</th>
						<th>供应商</th>
						<th>尺码</th>
						<th>颜色</th>
						<th>货架</th>
						<th>库存件数</th>
						<th>售出件数</th>
						<th>入库价格</th>
						<th>出库价格</th>
						<th>销售价格</th>
						<th>活动优惠</th>
						<th>实际销售价格</th>
						<th>渠道</th>
					</tr>
				</thead>
				<tbody>
				<c:forEach items="${productManage.columnList}" var="productManage" varStatus="vs">
					<tr>
						<td>
							<input type="hidden" name="columnList[${vs.index}].id" value="${productManage.id}"/>
							<input type="hidden" name="columnList[${vs.index}].categoryId" value="${productManage.categoryId}"/>
							<input type="hidden" name="columnList[${vs.index}].supplierId" value="${productManage.supplierId}"/>
							<input type="hidden" name="columnList[${vs.index}].sizeId" value="${productManage.sizeId}"/>
							<input type="hidden" name="columnList[${vs.index}].colorId" value="${productManage.colorId}"/>
							<input type="hidden" name="columnList[${vs.index}].instockDate" value="<fmt:formatDate value="${productManage.instockDate}" pattern="yyyy-MM-dd HH:mm:ss"/>"/>
							<input type="text" name="columnList[${vs.index}].category.name" value="${productManage.category.name}" maxlength="64" class="required" style="width:100px;" readonly="true"/>
						</td>
						<td>
							<input type="text" name="columnList[${vs.index}].productCode" value="${productManage.productCode}" maxlength="64" class="required" style="width:100px;" readonly="true"/>
						</td>
						<td>
							<input type="text" name="columnList[${vs.index}].supplier.name" value="${productManage.supplier.name}" maxlength="64" class="required" style="width:100px;" readonly="true"/>
						</td>
						<td>
							<input type="text" name="columnList[${vs.index}].size.name" value="${productManage.size.name}" maxlength="64" class="required" style="width:70px;" readonly="true"/>
						</td>
						<td>
							<input type="text" name="columnList[${vs.index}].color.name" value="${productManage.color.name}" maxlength="32" class="required" style="width:70px;" readonly="true"/>
						</td>
						<td>
							<input type="text" name="columnList[${vs.index}].gridNo" value="${productManage.gridNo}" maxlength="32" style="width:100px;" readonly="true"/>
						</td>
						<td>
							<input type="text" name="columnList[${vs.index}].num" value="${productManage.num}" maxlength="5" class="required" style="width:70px;" readonly="true"/>
						</td>
						<td>
							<input type="text" name="columnList[${vs.index}].amount" value="${productManage.amount}" maxlength="5" class="required digits" style="width:70px;"/>
						</td>
						<td>
							<input type="text" name="columnList[${vs.index}].price" value="${productManage.price}" class="required" style="width:70px;" readonly="true"/>
						</td>
						<td>
							<input type="text" name="columnList[${vs.index}].outstockPrice" value="${productManage.price}" class="required number" style="width:70px;"/>
						</td>
						<td>
							<input type="text" name="columnList[${vs.index}].salesPrice" id="salesPrice${vs.index}" value="${productManage.salesPrice}" class="required number" onblur="changePrice('#salesPrice${vs.index}','#activePrice${vs.index}','#realPrice${vs.index}')" style="width:70px;"/>
						</td>
						<td>
							<input type="text" name="columnList[${vs.index}].activePrice" id="activePrice${vs.index}" value="${productManage.activePrice}" class="required number" onblur="changePrice('#salesPrice${vs.index}','#activePrice${vs.index}','#realPrice${vs.index}')" style="width:70px;"/>
						</td>
						<td>
							<input type="text" name="columnList[${vs.index}].realPrice" id="realPrice${vs.index}" value="${productManage.salesPrice}" class="required number" readonly="true" style="width:70px;"/>
						</td>
						<td>
							<select name="columnList[${vs.index}].canalId" id="canalId${vs.index}" class="input-medium required">
									<c:forEach items="${fns:getCanal() }" var="v">
										<option value="${v.value}">${v.label}</option>
									</c:forEach>
								</select>
							</td>
						<%-- <td>
							<form:select path="columnList[${vs.index}].materialDetails" class="input-medium">
								<form:option value="">请选择</form:option>
								<form:options items="${fns:getMateria()}" itemLabel="label" itemValue="value" htmlEscape="false"/>
							</form:select>
						</td>
						<td>
							<input type="text" name="columnList[${vs.index}].materialAmount" value="${productManage.materialAmount}"  maxlength="200" class="input-min digits" style="width:100px;"/>
						</td> --%>
					</tr>
				</c:forEach>
				</tbody>
			</table>
		</div>
		<div class="control-group">
			<label class="control-label">物料添加：</label>
			<div class="controls">
					<table id="contentTable" class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>
								<th class="hide"></th>
								<th>物料明细</th>
								<th>物料数量</th>
								<th>删除</th>
							</tr>
						</thead>
						<tbody id="eventMaterialList">
						</tbody>
						<shiro:hasPermission name="ka:productManage:edit"><tfoot>
							<tr><td colspan="10"><a href="javascript:" onclick="addRow('#eventMaterialList', eventMaterialRowIdx, eventMaterialTpl);  eventMaterialRowIdx = eventMaterialRowIdx + 1;" class="btn">新增</a></td></tr>
						</tfoot></shiro:hasPermission>
					</table>
					<script type="text/template" id="eventMaterialTpl">//<!--
						<tr id="eventMaterialList{{idx}}">
							<td class="hide">
								<input id="eventMaterialList{{idx}}_delFlag" type="hidden" value="{{row.delFlag}}"/>
							</td>
							<td>
								<select name="eventMaterialList[{{idx}}].id" data-value="{{row.id}}">
									<option value=""></option>
									<c:forEach items="${fns:getMateria() }" var="v">
										<option value="${v.value}">${v.label}</option>
									</c:forEach>
								</select>
							</td>
							<td>
								<input id="eventMaterialList{{idx}}_amount" name="eventMaterialList[{{idx}}].amount" type="text" value="{{row.amount}}" maxlength="255" class="input-small"/>
							</td>
							<shiro:hasPermission name="ka:productManage:edit"><td class="text-center" width="10">
								{{#delBtn}}<span class="close" onclick="delRow(this, '#eventMaterialList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
							</td></shiro:hasPermission>
						</tr>//-->
					</script>
					<script type="text/javascript">
						var eventMaterialRowIdx = 0, eventMaterialTpl = $("#eventMaterialTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
						$(document).ready(function() {
							var data = ${fns:toJson(productManage.eventMaterialList)};
							for (var i=0; i<data.length; i++){
								addRow('#eventMaterialList', eventMaterialRowIdx, eventMaterialTpl, data[i]);
								eventMaterialRowIdx = eventMaterialRowIdx + 1;
							}
						});
					</script>
			</div>
		</div>
		<div class="form-actions">
			<shiro:hasPermission name="ka:productManage:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
		</div>
	</form:form>
</body>
</html>